import re
import traceback
from bs4 import BeautifulSoup
from crawlers.utils import fetch_webpage
from crawlers.constants import (
    BAIDUTIEBA_HOT_TOPIC_URL,
    BAIDUTIEBA_REQUEST_HEADERS,
)

def _process_hot_value(raw_hot_value):
    """处理热搜热度值，将其转换为整数

    Parameters:
        raw_hot_value: 原始热度值字符串

    Returns:
        处理后的整数热度值
    """
    unit_map = {
        'K': 1000,
        'W': 10000,
        'B': 100000000,
    }

    pattern = re.compile(r'(?P<num>\d+(\.\d+)?)(?P<unit>[KWB]?)')
    match = pattern.search(raw_hot_value)
    if match:
        num = float(match.group('num'))
        unit = match.group('unit')
        if unit in unit_map:
            return int(num * unit_map[unit])
        else:
            return int(num)
    else:
        return 0


def get_hot_topics_from_baidutieba():
    """从百度贴吧获取热议榜数据

    Returns:
        热议榜数据列表，每个热议榜包含序号、标题、链接、热度和简介信息、以及图片链接
    """
    try:
        webpage = fetch_webpage(BAIDUTIEBA_HOT_TOPIC_URL, BAIDUTIEBA_REQUEST_HEADERS)
        soup = BeautifulSoup(webpage, 'html.parser')
        hot_topics = []
        hot_topic_elements = soup.select('.topic-top-list .topic-top-item')
        
        for index, element in enumerate(hot_topic_elements):
            hot_topic_info = {}
            hot_topic_info['order'] = index + 1
            info_a_element = element.select_one('.topic-info .topic-name a.topic-text')
            hot_topic_info['title'] = info_a_element.text
            hot_topic_info['url'] = info_a_element.get('href')
            info_span_element = element.select_one('span.topic-num')
            hot_topic_info['hot'] = _process_hot_value(info_span_element.text)
            info_p_element = element.select_one('p.topic-top-item-desc')
            hot_topic_info['intro'] = info_p_element.text
            info_img_element = element.select_one('img')
            hot_topic_info['photo_url'] = info_img_element.get('src')
            
            hot_topics.append(hot_topic_info)

        return hot_topics
    except Exception as e:
        print("百度贴吧热议榜数据获取失败，", e)
        print("详细错误信息:")
        traceback.print_exc()
        raise